package com.easychat.entity.enums;

import org.apache.commons.lang3.StringUtils;

public enum UserTypeEnum {
    USER(0, "U", "好友"),
    GROUP(1, "G", "群");

    //类型
    private Integer type;
    //前缀
    private String prefix;
    //描述
    private String description;

    UserTypeEnum(Integer type, String prefix, String description) {
        this.type = type;
        this.prefix = prefix;
        this.description = description;
    }

    public Integer getType() {
        return type;
    }

    public String getPrefix() {
        return prefix;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 返回指定名称的枚举对象
     * @param name
     * @return
     */
    public static UserTypeEnum getByName(String name) {
        try {
            if (StringUtils.isEmpty(name)) {
                //传入字符串不合法，返回null
                return null;
            }
            return UserTypeEnum.valueOf(name.toUpperCase());//如果名称转为大写也没有相匹配的枚举对象，会产生异常
        } catch (IllegalArgumentException e) {
            //产生异常，返回null
            return null;
        }
    }

    /**
     * 返回指定前缀的枚举对象
     * @param prefix 待获取前缀的字符串
     * @return
     */
    public static UserTypeEnum getByPrefix(String prefix) {
        try {
            if (StringUtils.isEmpty(prefix)) {
                //传入字符串不合法，返回null
                return null;
            }
            for (UserTypeEnum userTypeEnum : UserTypeEnum.values()) {
                if (prefix.substring(0,1).equals(userTypeEnum.getPrefix())) {
                    return userTypeEnum;
                }
            }
            return null;
        } catch (Exception e) {
            //产生异常，返回null
            return null;
        }
    }
}
